<template>
  <ee-popup :on-show="onShow" :on-close="onClose" ref="root" class="ee-dialog">
    <slot name="header">
      <div class="header" v-if="title || closeable">

        <h3 class="title" v-if="title">
          <slot name="title">
            {{ title }}
          </slot>
        </h3><template v-else />

        <ee-button v-if="closeable" plain class="closeable" @click="setResult(false)">
          <slot name="close">
            <ee-shape shape="close" />
          </slot>
        </ee-button>
      </div><template v-else />
    </slot>
    <div class="content">
      <slot name="icon">
        <i v-if="icon" :class="'icon iconfont ' + icon"></i><template v-else />
      </slot>

      <slot name="text">
        <div v-if="html" class="text" v-html="html"></div><template v-else />
        <div v-if="text" class="text" v-text="text"></div><template v-else />
      </slot>

      <slot></slot>
    </div>
    <slot name="footer" :setResult="setResult">
      <footer v-if="confirm || cancel">
        <ee-button class="cancel" v-if="cancel" @click="setResult(false)">
          <slot name="cancel">
            {{ cancelText }}
          </slot>
        </ee-button><template v-else />
        <i class="line" v-if="confirm && cancel"></i><template v-else />
        <ee-button class="confirm" v-if="confirm" @click="setResult(true)">
          <slot name="confirm">
            {{ confirmText }}
          </slot>
        </ee-button><template v-else />
      </footer><template v-else />
    </slot>
  </ee-popup>
</template>

<script type="text/javascript">
import { props } from './ee-dialog'
/**
 * 插槽
 * @property {} header - 头部，包括标题和关闭按钮
 * @property {} header.title - 标题
 * @property {} header.close - 关闭按钮
 * 
 * @property {} icon - 左侧图标
 * @property {} text - 右侧文字
 * @property {} default - 内容，在右侧文字后面
 * 
 * @property {{setResult: function(boolean)}} footer - 底部，包含取消和确认，setResult可以设置对话框的结果并关闭对话框
 * @property {} footer.cancel - 取消按钮内容
 * @property {} footer.confirm - 确认按钮内容
 */
export default {
  name: "ee-dialog",
  data() {
    return {
      result: false,
      _duration: undefined,
    }
  },
  props,
  methods: {
    onShow() {
      if (this.duration > 0) {
        if (this._duration)
          clearTimeout(this._duration);
        this._duration = setTimeout(() => this.setResult(false), this.duration)
      }
    },
    async onClose() {
      const r = this.result;
      let result;
      if (this.onResult) {
        result = await this.onResult?.(r, this)
        if (!(result || result == undefined))
          return false;
      }

      if (r) {
        if (this.onConfirm)
          result = await this.onConfirm?.(r, this);
      } else {
        if (this.onCancel)
          result = await this.onCancel?.(r, this);
      }
      return result;
    },
    setResult(result) {
      this.result = result;
      this.$refs.root.modelValue = false;
    }
  },
}
</script>

<style lang="scss">
.ee-dialog {
  max-width: 88vw;
  max-height: 88vh;
  min-height: 6vh;
  min-width: min(20em, 88vw);
  background-color: #fff;
  border-radius: .5em;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  // padding-top: 1em;

  .header {
    position: relative;
    margin-top: 1em;

    .title {
      text-align: center;
      color: var(--ee-dialog-title);
      padding: 0 1em;
    }

    .closeable {
      position: absolute;
      right: 0.5em;
      top: -0.25em;
      font-size: 1em;
      border: none;
      padding: 0.4em;
    }
  }

  .content {
    flex: 1;
    overflow: auto;
    padding: 2em 1.5em;
    color: var(--ee-dialog-text);
    display: flex;
    justify-content: center;

    .iconfont {
      font-size: 1.5em;
      margin-right: .4em;
    }

    // icon设置成info, warning, error, success时的样式
    .icon {
      color: #fff;
      width: 1.25em;
      height: 1.25em;
      border-radius: 50%;
    }

    .text {
      text-align: center;
      flex: 1;
      line-height: 1.5;
    }
  }

  footer {
    display: flex;
    position: relative;
    border-top: 1px solid var(--ee-dialog-line);

    .line {
      width: 1px;
      border-left: 1px solid var(--ee-dialog-line);
    }

    button {
      border: none;
      border-radius: 0;
      flex: 1;
      padding: 0;
      line-height: 3;
    }

    .confirm {
      color: var(--ee-dialog-confirm);
    }
  }
}
</style>